﻿using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
public class OCharacterController : MonoBehaviour {
    [Range(0.5f, 8f)]
    public float jump_height = 4f;
    [Range(0.05f,3f)]
    public float jump_time = 1f;
    public float speed_run = 4f;
    public float speed_rotate = 720f;

    float y_speed = 0;

    Vector3 _movement;
    CharacterController _cc;
    Animator _ator;
    GameObject _camera;
	// Use this for initialization
	void Start () {
        _ator = GetComponent<Animator>();
        //_camera = GameObject.Find("Main Camera");
        _camera = Camera.main.gameObject;
        _cc = GetComponent<CharacterController>();
        _movement = Vector3.zero;

        // 
        //transform.position = new Vector3(transform.position.x, 15f, transform.position.z);

        //if(Terrain.activeTerrain)
        //{
        //    // A little height, so character will not drop down.
        //    var h = Terrain.activeTerrain.SampleHeight(transform.position);
        //    transform.position = new Vector3(transform.position.x, h + 0.01f, transform.position.z);
        //}
    }
	
	// Update is called once per frame
	void Update () {
        if(GetInput())
        {
            Move();
        }
        else
        {
            Stand();
        }

        // 跳跃相关
        // init_jump_speed = 2*jump_height/jump_time
        // gravity = 2*jump_height/jump_time/jump_time

        if (Input.GetKey(KeyCode.Space))
        {
            y_speed = 2 * jump_height / jump_time;
        }
        else
        {
            float gravity = 2 * jump_height / jump_time / jump_time;
            y_speed -= gravity * Time.deltaTime;
        }
        var move = new Vector3(0, y_speed * Time.deltaTime, 0);
        var flag = _cc.Move(move);
        if ((flag & CollisionFlags.CollidedBelow) != 0)
        {
            y_speed = 0;
        }
    }

    bool GetInput()
    {
        int nh = 0; int nv = 0;

        if (Input.GetKey(KeyCode.W)) nv++;
        if (Input.GetKey(KeyCode.S)) nv--;
        if (Input.GetKey(KeyCode.A)) nh--;
        if (Input.GetKey(KeyCode.D)) nh++;
        if (nv == 0 && nh == 0)
            return false;

        Vector3 dir = _camera.transform.forward;
        if(Mathf.Abs(Vector3.Dot(Vector3.up,_camera.transform.forward)) > 0.9)
        {
            dir = _camera.transform.up;
        }
        dir.y = 0;
        var q = Quaternion.FromToRotation(Vector3.forward, dir);
        _movement.x = nh;
        _movement.z = nv;
        _movement.y = 0;
        _movement = q * _movement.normalized;
        //if(y_speed <=0)
        // _movement.y = -1;// 垂直移动单独管理，来支持跳跃

        _movement *= speed_run;
        return true;
    }

    void Move()
    {
        var dir = _movement;
        dir.y = 0;
        var dir_q = Quaternion.LookRotation(dir);
        //dir_q = Quaternion.Euler(Vector3.up);
        //dir.y = 0;
        transform.rotation = Quaternion.RotateTowards(transform.rotation, dir_q, Time.deltaTime * speed_rotate);
        //transform.rotation = dir_q;

        _cc.Move(Time.deltaTime * _movement);
        _movement = Vector3.zero;
        SetTrigger("ToWalk");
    }

    void Stand()
    {
        SetTrigger("ToIdle");
    }

    string _trigger_name = "ToIdle";
    void SetTrigger(string name)
    {
        if (_ator == null) return;
        if (_trigger_name == name) return;
        if (_ator.IsInTransition(0)) return;
        _trigger_name = name;
        _ator.SetTrigger(name);
    }

    void Run()
    {
        _cc.Move(_movement);
        _movement = Vector3.zero;
    }
}
